<?php

namespace ticket\modules\frontend\models;

use Yii;

/**
 * This is the model class for table "tk_error_msg".
 *
 * @property int $id
 * @property string $url
 * @property string $code
 * @property string $msg 错误详情
 * @property string $data 数据
 * @property string $operation 操作用户
 * @property string $create_time 创建时间
 */
class ErrorMsg extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_error_msg';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['url', 'msg', 'create_time'], 'required'],
            [['msg', 'data'], 'string'],
            [['create_time'], 'safe'],
            [['url'], 'string', 'max' => 255],
            [['code'], 'string', 'max' => 8],
            [['operation'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'url' => 'Url',
            'code' => 'Code',
            'msg' => 'Msg',
            'data' => 'Data',
            'operation' => 'Operation',
            'create_time' => 'Create Time',
        ];
    }

    public static function setInfo($url,$code,$msg,$data,$openid = '')
    {

        $data = [
            'url' => $url,
            'code' => $code,
            'msg' => $msg,
            'data' => json_encode($data,JSON_UNESCAPED_UNICODE),
            'operation' => $openid,
            'create_time' => date("Y-m-d H:i:s"),
        ];
        $model = new self();
        $model->setAttributes($data);
        if ($model->save(0)){
            return true;
        }
        return false;


    }


    public function getDetails($payNumber)
    {
        $query = self::find()->where(['like','msg',$payNumber])->andFilterWhere(['url' => '//frontend/pay-callback/ticket-callback']);
        $info = $query->orderBy(['id' => SORT_DESC])->asArray()->one();
        return $info;
    }
}
